import Vue from 'vue'


import VueRouter from 'vue-router'
import Login from '../views/login.vue'
import Registor from '../views/registor.vue'
import Home from '../views/home.vue'
import DataManagement from '../components/dataManagement.vue'
import userList from '../components/userList.vue'
import userOperate from '../components/userOperate.vue'
import pictureList from '../components/pictureList.vue'
import pictureOperate from '../components/pictureOperate.vue'
import typeList from '../components/typeList.vue'
import foodList from '../components/foodList.vue'
import foodOperate from '../components/foodOperate.vue'
import orderList from '../components/orderList.vue'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/registor',
    component: Registor
  },
  {
    path: '/home',
    component: Home,
    redirect: '/datamanagement',
    children: [{
      path: '/datamanagement',
      component: DataManagement
    }, {
      path: '/userList',
      component: userList
    }, {
      path: '/userOperate',
      component: userOperate,
      meta: ['超级管理员']
    }, {
      path: '/pictureList',
      component: pictureList
    }, {
      path: '/pictureOperate',
      component: pictureOperate
    }, {
      path: '/typeList',
      component: typeList
    }, {
      path: '/foodList',
      component: foodList
    }, {
      path: '/foodOperate',
      component: foodOperate
    }, {
      path: '/orderList',
      component: orderList
    }]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路经
  // from 代表从那个路径跳转而来
  // next 是一个函数  表示放行   next('/login')=>表示强制跳转
  if (to.path === '/login') return next()
  if (to.path === '/registor') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有 tokenStr 强制跳转
  if (!tokenStr) return next('/login')
  // 解析token
  // const decode = jwtDecode(tokenStr)
  // console.log(decode)
  // if (decode.scope !== '超级管理员') {
  //   if (to.path === '/userOperate') {
  //     return next('/userList')
  //   } else if (to.path === '/pictureOperate') {
  //     return next('/pictureList')
  //   } else if (to.path === '/foodOperate') {
  //     return next('/foodList')
  //   }
  // }
  next()
})


export default router
